// 全局参数
var param = {
	userID : 0,
	userCode : "",
	userName : ""

}

//初始化变量
function initParam() {
	
	//param.userCode = request("userCode");
	
}

//左侧菜单(待调整)
/*
var leftMenuJson = [ 
	{
	level : "1",
	name : "系统管理",
	code : "code1",
	url : "",
	childs: [
		{
			level : "2",
			name : "字典管理",
			code : "code2",
			url : "/panda/dict/dictList.html"
		}]
	}
];*/

//初始化layer内容
function initLayer(){
	
	layui.use('element', function(){
        var $ = layui.jquery, element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
        
        initLeftMenu(element);
        
        //initLayerTab(element);//动态时，需要放在数据后面加载
        
    });
}

//左侧导航栏初始化
function initLeftMenu(element){
	//静态demo
	/*var menuList = leftMenuJson;
	initMenuData(leftMenuJson);
	element.render('nav');*/
	
	$ajax({
		url : config.servicePath + '/rentHouse/menu/getMenuList',
		success : function(result){
			var code = result.code;
			//console.log(result);
			if(code=="0"){
				var menuList = result.data;
				initMenuData(menuList);
				element.render('nav');
				
				initLayerTab(element);
			}
		}
	});
	
}

//初始化tab组件
function initLayerTab(element){
	//触发事件
    var active = {
      tabAdd: function(othis){////增加指定Tab项
    	  var $othis = $(othis[0]);
    	  var code = $othis.attr("code");
    	  var exist = $("li[lay-id='"+code+"']").length;
    	  var h = $(document).height()*0.76;
    	  if(exist<1){
        	  var title = $othis.text();
        	  var url = $othis.attr("url");
        	  var content = '<div class="layui-tab-item layui-show">';
        	  content += '<iframe src="' + url + '" frameborder="0" class="layadmin-iframe"  height="'+h+'"></iframe>';
        	  content += '</div>';
        	  //新增一个Tab项
        	  element.tabAdd('layerTab', {
        		  title: title,
        		  content: content,
        		  id: code
        	  });
    	  }
		  element.tabChange('layerTab', code); //切换到：用户管理
      }
      ,tabDelete: function(othis){//删除指定Tab项
    	  var $othis = $(othis[0]);
    	  var code = $othis.attr("code");
    	  element.tabDelete('layerTab', code);
    	  othis.addClass('layui-btn-disabled');
      }
    };
    
    $('.site-demo-active').on('click', function(){
    	var othis = $(this), type = othis.data('type');
    	active[type] ? active[type].call(this, othis) : '';
    });
}

//装配菜单模板
function initMenuData(data){
	var ul = $("<ul class='layui-nav layui-nav-tree' lay-filter='test'></ul>");
    for(var i=0;i < data.length;i++){
        var node = data[i];
        var li = $("<li class='layui-nav-item'></li>");
        var a = $("<a class='' href='javascript:;'>"+node.name+"</a>");
        li.append(a);
        //获取子节点
        var childs = node.childs;
        if(childs.length>0){
            a.append("<span class='layui-nav-more'></span>");
            var dl = $("<dl class='layui-nav-child'></dl>");
            for (var y in childs) {
                var dd = $("<dd><a class='site-demo-active' data-type='tabAdd' url='" + childs[y].url + "' code='"+childs[y].code+"'>"+childs[y].name+"</a></dd>");
                //判断选中状态
                dl.append(dd);
            }
            li.append(dl);
        }
        ul.append(li);
    }
    $(".layui-side-scroll").append(ul);
}

//初始化
function initHome(){
	
	initParam();//初始化变量
	
	initLayer();//初始化layerUI
	
}
